﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListView控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listView1.View = View.Details;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                MessageBox.Show(item.Text);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Columns.Clear();
            listView1.Items.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColumnHeader Header1 = new ColumnHeader();
            Header1.Text = "Name";
            Header1.TextAlign = HorizontalAlignment.Center;
            Header1.Width = 100;
            listView1.Columns.Add(Header1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listView1.Columns.Add("Age",40,HorizontalAlignment.Center);
            listView1.Columns.Add("Job", 80, HorizontalAlignment.Center);
            listView1.Columns.Add("Native place", 60, HorizontalAlignment.Center);
            listView1.Columns.Add("Telephone", 80, HorizontalAlignment.Center);
            button3.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Columns.Remove(listView1.Columns[0]);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            listView1.BeginUpdate();
            listView1.Items.Clear();
            listView1.Items.Add("row1", "张三", 0);
            listView1.Items["row1"].SubItems.Add("21");
            listView1.Items["row1"].SubItems.Add("无");
            listView1.Items["row1"].SubItems.Add("北京");
            listView1.Items["row1"].SubItems.Add("1234");
            listView1.Items.Add("row2", "李四", 1);
            listView1.Items["row2"].SubItems.Add("23");
            listView1.Items["row2"].SubItems.Add("工人");
            listView1.Items["row2"].SubItems.Add("南京");
            listView1.Items["row2"].SubItems.Add("1233");
            listView1.EndUpdate();
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if(i%2==0)
                {
                    listView1.Items[i].BackColor = Color.Blue;
                }
            }

            listView1.GridLines = true;
            listView1.View=View.Details;
            listView1.LabelEdit = true;
            listView1.Scrollable = true;
            listView1.HeaderStyle=ColumnHeaderStyle.Clickable;
        }
    }
}
